package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description
 * @author: ZXX-挚爱
 * @time: 2023/6/12 10:41
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /** @author ZXX-挚爱
    * @Description 购物车新增
    * @Dete 11:00 2023/6/12
    * @Param [shoppingCartDTO]
    * @return void
    **/

    @Override
    public void addShopingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //只能查询自己购物车里面数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //考虑商品是否已经在购物车里面，如果在 则进行商品基数，
        // 如果不在则对购物车进行插入数据的操作
        //①首先遍历购物车集合
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //②开始进行判断 购物车数据大小为1 则里面已经有数据
        if (shoppingCartList!=null && shoppingCartList.size()==1){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else { //
            //考虑如果已经存在，就更新数量，数量加1
            //判断当前添加到购物车的是菜品还是套餐
            //毕竟购物车里面不是套餐就是菜品     通过dishId和setmealId是否为空来判断
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId!=null){//购物车里面   dishId不为空，则是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {//购物车里面   setmealId不为空，则是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /** @author ZXX-挚爱
    * @Description 查看购物车
    * @Dete 14:55 2023/6/12
    * @Param
    * @return java.util.List<com.sky.entity.ShoppingCart>
    **/

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /** @author ZXX-挚爱
    * @Description 清空购物车
    * @Dete 15:24 2023/6/12
    * @Param
    * @return void
    **/

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userId);
    }

    @Override
    public void sub(ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCartMapper.sub(shoppingCart);
    }


}
